//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.product.system.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.product.system.domain.SysDictData;
import com.product.system.domain.SysPost;
import com.product.system.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/dict")
public class SysDictDataController {
    @Autowired
    SysDictDataService sysDictDataService;


    @PostMapping("dictlist")
    public List<SysDictData> list( @RequestBody SysDictData sysDictData) {

        List<SysDictData> list = sysDictDataService.list(sysDictData);

        return  list;
    }
    @PostMapping("dictlistall")
    public PageInfo<SysDictData> postlistall(@RequestParam(name = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum,3);

        List<SysDictData> list = sysDictDataService.listAll();
        PageInfo<SysDictData> info = new PageInfo<>(list);
        return  info;
    }

    @PostMapping("add")
    public HashMap<String, Object> add(@RequestBody  SysDictData sysDictData) {
        Boolean save = sysDictDataService.save(sysDictData);
        HashMap<String, Object> map = new HashMap();
        String code = "";
        String msg = "";
        if (save) {
            map.put("200", code);
            map.put("添加成功", msg);
        } else {
            map.put("500", code);
            map.put("添加失败", msg);
        }

        return map;
    }
    @GetMapping("excel")
    public  void excel(HttpServletResponse response) throws Exception {
        String fileName = URLEncoder.encode("字典信息", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<SysDictData> list = sysDictDataService.listAll();
        EasyExcel.write(response.getOutputStream(), SysPost.class).sheet("字典信息").doWrite(list);


    }
    @PostMapping("update")
    public HashMap<String, Object> update(@RequestBody  SysDictData sysDictData) {
        Boolean save = sysDictDataService.update(sysDictData);
        HashMap<String, Object> map = new HashMap();
        String code = "";
        String msg = "";
        if (save) {
            map.put("200", code);
            map.put("添加成功", msg);
        } else {
            map.put("500", code);
            map.put("添加失败", msg);
        }

        return map;
    }

    @PostMapping("delete")
    public HashMap<String, Object> delete(@RequestBody  SysDictData sysDictData) {
        if (sysDictData.getStatus().equals("1")){
            sysDictData.setStatus("0");
        }else{
            sysDictData.setStatus("1");
        }
        Boolean save = sysDictDataService.updatebyid(sysDictData);
        HashMap<String, Object> map = new HashMap();
        String code = "";
        String msg = "";
        if (save) {
            map.put("200", code);
            map.put("添加成功", msg);
        } else {
            map.put("500", code);
            map.put("添加失败", msg);
        }

        return map;
    }
}

